package entries;

import java.util.ArrayList;

import javax.swing.BoxLayout;

import Models.Addition;
import Models.Ingredient;
import Models.Recipe;
import Views.RecipePanel;

public class MaltList extends AdditionList{


	private static final long serialVersionUID = 1L;
	private boolean mode; //read or write mode
	private ArrayList<Addition> additions;  //all additions to display
	private ArrayList<Addition> deleted;  //entries that were deleted
	private ArrayList<Addition> added;  //entries that were added
	private ArrayList<String> allMalts; //contains all the malts, used to populate the JComboBoxes
	
	public MaltList(boolean mode) {
		this.mode = mode;
		deleted = new ArrayList<Addition>();
		added = new ArrayList<Addition>();
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
	}
	


	@Override
	public void updateContents(Recipe newRecipe) {
		parseRecipe(newRecipe);
		this.removeAll();
		deleted.clear();
		added.clear();
		for(Addition add: additions) {
			this.add(new MaltEntry(this, add, mode));
		}
		if(this.mode == RecipePanel.EDIT) {
			//create a method that makes an empty entry, append this new entry to the bottom after an entry has been added.
			this.add(new MaltEntry(this, new Addition(), mode)); //add an empty addition with a 0 recipeID
		}
	}
	
	/**
	 * Removes an entry from the view, but does not delete from the database until "save" is pressed.
	 * @param old
	 */
	public void removeEntry(MaltEntry old) {
		deleted.add(old.getAddition());
		this.remove(old);
		revalidate();
	}
	
	public void updateMaltList(String newMalt) {
		
	}
	
	private void parseRecipe(Recipe newRecipe){
		additions = new ArrayList<Addition>();
		if(!newRecipe.getIdRecipe().equals(Recipe.EMPTY)) {  //If this is an actual recipe
			ArrayList<Addition> addList = newRecipe.getAdditions();
			for(Addition add: addList) {
				if(add.getTheIngredient().getIngredientType().equals(Ingredient.MALT)){
					additions.add(add);
				}
			}
		}
	}
}
